package free.emaranhado.lineparams;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import free.emaranhado.lineparams.model.ConductorTypes;
import free.emaranhado.lineparams.util.Params;
import free.emaranhado.lineparams.util.XMLParser;

public class ParamTFview extends Activity implements OnClickListener {
	private String[] meterUnities = { "m", "cm", "in", "ft", "mi", };
	private Button mOk;
	private int positionConduct;
	private ConductorTypes CondTypes = null;
	private Params mParams = new Params();
	private Spinner sp4;
	private Spinner sp5;
	private Spinner sp6;

	private double d12;
	private double d13;
	private double d23;

	private int pos12;
	private int pos13;
	private int pos23;

	private EditText dd12;
	private EditText dd13;
	private EditText dd23;
	
	private double Ceq;
	private double Leq;
	private double rc20;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.param_tf_sc);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, meterUnities);
		sp4 = (Spinner) findViewById(R.id.spinner4);
		sp4.setAdapter(adapter);
		sp5 = (Spinner) findViewById(R.id.spinner5);
		sp5.setAdapter(adapter);
		sp6 = (Spinner) findViewById(R.id.spinner6);
		sp6.setAdapter(adapter);

		mOk = (Button) findViewById(R.id.ok);
		mOk.setOnClickListener(this);


		try {
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser spXml = spf.newSAXParser();
			XMLReader xmlr = spXml.getXMLReader();

			AssetManager assetManager = getAssets();
			InputStream inputStream = null;
			try {
				inputStream = assetManager.open("table_acsr.xml");
			} catch (IOException e) {
				Log.e("URL", "IOException");
			}

			XMLParser XMLParser = new XMLParser();
			xmlr.setContentHandler(XMLParser);
			xmlr.parse(new InputSource(inputStream));

		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Get result from XMLParser */
		CondTypes = XMLParser.CondTypes;

		/** populate Spinner with conductor list */
		ArrayAdapter<String> conductor = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line,
				CondTypes.getCode_word());
		Spinner cdt = (Spinner) findViewById(R.id.spinner1);
		cdt.setAdapter(conductor);
		cdt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				positionConduct = pos;
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		// get the unity

		sp4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				Log.i("pos", "pos: " + pos);
				pos12 = pos;

			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		// get the unity

		sp5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				Log.i("pos", "pos: " + pos);
				pos13 = pos;

			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		// get the unity
		sp6.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				pos23 = pos;

			}

			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		dd12 = (EditText) findViewById(R.id.d12);
		dd13 = (EditText) findViewById(R.id.d13);
		dd23 = (EditText) findViewById(R.id.d23);

		if (dd12.getText().toString().length() == 0) {

			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setMessage("O campo D12 est� vazio");
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			});
			alertDialog.show();
		} else if (dd13.getText().toString().length() == 0) {

			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setMessage("O campo D13 est� vazio");
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			});
			alertDialog.show();
		} else if (dd23.getText().toString().length() == 0) {

			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setMessage("O campo D23 est� vazio");
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			});
			alertDialog.show();
		}

		else {
			GetParamsTask task = new GetParamsTask();
			task.execute();
			
			
	}
	}
	
	public class GetParamsTask extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			d12 = Double.parseDouble(dd12.getText().toString());
			d13 = Double.parseDouble(dd13.getText().toString());
			d23 = Double.parseDouble(dd23.getText().toString());

			double dm1 = Double.parseDouble(CondTypes.getGMR().get(
					positionConduct));
			
			rc20 = Double.parseDouble(CondTypes.getRac_20().get(
					positionConduct));
			// convert to meter
			
			rc20 = rc20/1609.344;

			dm1 = mParams.ft2mts(dm1);

			switch (pos12) {
			case 1:
				d12 = mParams.cm2mts(d12);
				break;
			case 2:
				d12 = mParams.in2mts(d12);
				break;
			case 3:
				d12 = mParams.ft2mts(d12);
				break;
			case 4:
				d12 = mParams.mi2mts(d12);
				break;
			default:
				break;
			}

			switch (pos13) {
			case 1:
				d13 = mParams.cm2mts(d13);
				break;
			case 2:
				d13 = mParams.in2mts(d13);
				break;
			case 3:
				d13 = mParams.ft2mts(d13);
				break;
			case 4:
				d13 = mParams.mi2mts(d13);
				break;
			default:
				break;
			}

			switch (pos23) {
			case 1:
				d23 = mParams.cm2mts(d23);
				break;
			case 2:
				d23 = mParams.in2mts(d23);
				break;
			case 3:
				d23 = mParams.ft2mts(d23);
				break;
			case 4:
				d23 = mParams.mi2mts(d23);
				break;
			default:
				break;
			}

			// get inductance by phase
			Leq = mParams.getInductanceTF(dm1, d12, d13, d23);
			Log.i("Diam", " d12=" + d12 + " d13=" + d13 + " d2=" + d23);
			
			Ceq = mParams.getCapacitanceTF(dm1, d12, d13, d23);
			// generate dialog with results
		
			
			
			
//			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
//			alertDialog.setTitle(R.string.app_name);
//			alertDialog.setMessage("A Indutancia por fase: " + Leq + " H/m \n Capacitancia Can=" + Ceq + " F/m");
//			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int which) {
//					return;
//				}
//			});
//			alertDialog.show();

			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			TextView cap = (TextView) findViewById(R.id.cap);
			TextView ind = (TextView) findViewById(R.id.ind);
			TextView rac = (TextView) findViewById(R.id.rac);
			
			cap.setText(String.valueOf(Ceq));
			ind.setText(String.valueOf(Leq));
			rac.setText(String.valueOf(rc20));
			
			super.onPostExecute(result);
		}
		 
	 }
}
